﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class DatabaseCheckPageVerifySetToNone : IssueTypeCheckBase
    {

        public DatabaseCheckPageVerifySetToNone()
        {
            IssueTypeId = 3012;
        }

        public override void Check()
        {
            try
            {
                // Database check Page Verify set to None
                //When PAGE_VERIFY is set to None on a writeable database it may prevent the early detection of I/O corruption issues.
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);



                var QryPageVerify = from d in dc.Databases

                                    where d.PageVerify == 0
                                    && d.SourceDatabaseId == null
                                    select d;

                foreach (var database in QryPageVerify)
                {
                    IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                    //Create new Issue

                    Issue issue = new Issue();
                    issue.IssueType = issueType;
                    issue.TargetId = database.DatabaseID;
                    issue.Description = issueType.Description.Replace("{Database}", database.DatabaseName);

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

    }
}
